#include "TitleScreenState.h"
#include "ScreenManager.h"
#include "Game.h"
#include "DX11HelpfulFunctions.h"
#include "TestState.h"
#include "Text.h"
#include "DX11TextureManager.h"
#include "Timer.h"
#include "LoadGameObjects.h"
#include "ConfigManager.h"
#include "InfoState.h"

TitleScreenState::TitleScreenState()
{
	m_screenSize.x = (float)TheScreenManager::Instance()->GetWindowWidth();
	m_screenSize.y = (float)TheScreenManager::Instance()->GetWindowHeight();
	m_mouseDown = false;

	t.SetFont("pixel_large", L"../../Assets/pixel2.png");
}

void TitleScreenState::Draw()
{
	DX11::ClearColour(0.0f, 0.0f, 0.0f, 1.0f);

	D3DXMATRIX m_view;
	D3DXMATRIX m_proj;

	D3DXMatrixLookAtLH(&m_view, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), 
		&D3DXVECTOR3(0.0f, 0.0f, 0.0f), 
		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
	
	D3DXMatrixOrthoLH(&m_proj, m_screenSize.x, m_screenSize.y, 0, 100);

	TheScreenManager::Instance()->SetViewProjMatrix(m_view, m_proj);
		
	TheScreenManager::Instance()->EnableBlending();
	TheObjMeshManager::Instance()->ChangeTexture("title_quad",
		TheDX11TextureManager::Instance()->UseTexture(L"../../Assets/title_image.png"));
	//TheObjMeshManager::Instance()->Translate("title_quad", D3DXVECTOR3(0.0f, -1.0f, rot));
	TheObjMeshManager::Instance()->Scale("title_quad", D3DXVECTOR3(m_screenSize.x, m_screenSize.y, 1.0f));
	//TheObjMeshManager::Instance()->Rotate("title_quad", M_PI_4, 1.0f, 0.0f, 0.0f);
	TheObjMeshManager::Instance()->Draw("title_quad");
	TheScreenManager::Instance()->DisableBlending();


	if (m_mouseDown)
	{	
		t.Update("Generating maze. . .");
		t.Translate(D3DXVECTOR3(0.0f, 0.0f, 0.0));
		t.SetColour(D3DXVECTOR4(1.0f, 1.0f, 1.0f, 0.0f));
		//t.Draw();

		TheScreenManager::Instance()->FlipBuffers();
		
		TheConfigManager::Instance()->Load("../../Config/config.info");
		LoadGameObjects(L"../../Game_Objects/test_gameobjects.txt");
		TheGame::Instance()->SetGameState(TheInfoState::Instance());
	}
	else
	{
		static float a = 0.0f;
		a += 2.0f * TheTimer::Instance()->GetDt();
		float alpha = sin(a) + 1.0f;
		t.Update("Click to start . . .");
		t.Translate(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
		alpha *= 0.4f;
		t.SetColour(D3DXVECTOR4(1.0f, 1.0f, 1.0f, alpha));
	
		//if (t.Intersected(m_convertedMousePos))
		//{
		//	t.Update("hit");
		//}

		t.Draw();
	}
	
}

void TitleScreenState::Update()
{

}

void TitleScreenState::OnActivated()
{
	TheEventPoller::Instance()->AddHandler(this);
	m_mouseDown = false;
}
void TitleScreenState::OnDeactivated()
{
	TheEventPoller::Instance()->RemoveHandler(this);
	m_mouseDown = false;
}

void TitleScreenState::OnKeyDownEvent(const WPARAM& event)
{			
	//char c = MapVirtualKey(event, MAPVK_VK_TO_CHAR);
	//if (c == '1')
	//{
	//	TheGame::Instance()->SetGameState(TheTestState::Instance());
	//}
}

void TitleScreenState::OnKeyUpEvent(const WPARAM& event)
{	
}

void TitleScreenState::OnMouseMotionEvent(const LPARAM& event)
{
	int x = LOWORD(event);
	int y = HIWORD(event);
	m_mousePos = D3DXVECTOR2(float(x), float(y));
	m_convertedMousePos.x = (m_mousePos.x - (m_screenSize.x * 0.5f)) / (m_screenSize.x * 0.5f);
	m_convertedMousePos.y = (m_mousePos.y - (m_screenSize.y * 0.5f)) / (m_screenSize.y * 0.5f);
}

void TitleScreenState::OnMouseButtonDownEvent(const MSG& event)
{
	m_mouseDown = true;
}

void TitleScreenState::OnMouseButtonUpEvent(const MSG& event)
{
}